package cn.xx.org.service.impl;

import cn.xx.basic.constant.PetHomeConstant;
import cn.xx.basic.service.impl.BaseServiceImpl;
import cn.xx.basic.exception.CustomException;
import cn.xx.basic.util.MD5Utils;
import cn.xx.basic.util.StrUtils;
import cn.xx.org.domain.Employee;
import cn.xx.org.domain.Shop;
import cn.xx.org.mapper.EmployeeMapper;
import cn.xx.org.mapper.ShopMapper;
import cn.xx.org.service.IShopService;
import cn.xx.user.domain.LoginInfo;
import cn.xx.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private JavaMailSender mailSender;
    @Override
    @Transactional
    public void settlement(Shop shop) throws CustomException {
        //校验前端传递的数据
        checkData(shop);
        //保存店铺
        shopMapper.save(shop); // 当保存了店铺之后， admin_id为空
        //获取员工对象
        Employee admin = shop.getAdmin();//拿到员工对象
        admin.setSalt(StrUtils.getComplexRandomString(10));
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword() + admin.getSalt()));
        //创建LoginInfo对象
        LoginInfo loginInfo = createLoginInfo(admin);
        loginInfoMapper.save(loginInfo);
        //设置登录信息
        admin.setLoginInfo(loginInfo);
        //给员工设置店铺
        admin.setShop(shop);//员工中shop是为空的，所以设置进去
        employeeMapper.save(admin);//保存员工了， 店铺就有值了
        //修改店铺
        shopMapper.update(shop);//当保存了员工之后，shop中的admin就有值了，所以要修改店铺，补全admin_id的值
    }

    @Override
    public void setPass(Long id) throws MessagingException {

    }

    /* 用employee新建LoginInfo */
    private LoginInfo createLoginInfo(Employee admin) {
        // 新建实体类对象
        LoginInfo loginInfo = new LoginInfo();
        // 调用工具类方法 将admin赋值给loginInfo
        BeanUtils.copyProperties(admin, loginInfo);
        // 设置登录的类型 用户/后台
        loginInfo.setType(PetHomeConstant.ADMIN);
        // 返回设置好的对象
        return loginInfo;
    }

    /*@Override
    public void setPass(Long id) throws MessagingException {
        Shop shop = shopMapper.findById(id);

        String email = employeeMapper.findShopId(id).getEmail();

        String email = employee.getEmail();

        //创建一个空的复杂邮件
        MimeMessage message = mailSender.createMimeMessage();
        //创建一个工具类，该工具类向空邮件中写内容
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true, "UTF-8");

        mimeMessageHelper.setFrom("284128040@qq.com");
        mimeMessageHelper.setTo(email);
        mimeMessageHelper.setSubject("<h1>同城交友，激情聊天</h1>");
        mimeMessageHelper.setText("<a href=http://localhost:8080/shop/"+id+">点我呀，有惊喜</a>" +
                "<img src='https://http://115.159.217.249:8888/group1/M00/00/3C/rBEAA1_wWZyAOhWEAAqF-3oKlII175.png'/><h1>哈哈哈哈</h1>",true);

        mailSender.send(message);


        shop.setState(1);
        shopMapper.update(shop);
    }*/

    @Override
    public void setReturn(Long id,String content) {
        System.out.println(id+"+++++++++"+content);
        Shop shop = shopMapper.findById(id);
        shop.setState(-1);
        shop.setContent(content);
        shopMapper.update(shop);
    }

    @Override
    public void setLoding(Long id, Shop para) {
        System.out.println(id+"+++++++++"+para);
        Shop shop = shopMapper.findById(id);
        shop.setState(1);
        shopMapper.update(shop);
    }

    @Override
    public void setopen(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setState(2);
        shopMapper.update(shop);
    }

    private void checkData(Shop shop) throws CustomException {
        if(StringUtils.isEmpty(shop.getName())) {
            throw new CustomException("店铺名为空！");
        }
        if(StringUtils.isEmpty(shop.getTel())) {
            throw new CustomException("座机号为空！");
        }
        if(StringUtils.isEmpty(shop.getAddress())) {
            throw new CustomException("店铺地址为空！");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getUsername())) {
            throw new CustomException("店铺管理人员为空！");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getPhone())) {
            throw new CustomException("手机号为空！");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getEmail())) {
            throw new CustomException("邮箱为空！");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getPassword())) {
            throw new CustomException("密码为空!");
        }
        //根据店铺名查询店铺对象 loadByName
        Shop s = shopMapper.loadByName(shop.getName());
        if (s != null) {
            throw new CustomException("店铺名已存在");
        }
        // 根据用户名查询员工对象 loadbyUsername
        Employee employee = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
        if (employee != null) {
            throw new CustomException("用户已存在");
        }
    }


}
